package com.yhsimon.controller;


import com.yhsimon.entity.Account;
import com.yhsimon.repository.AdminRepository;
import com.yhsimon.repository.UserRepository;
import com.yhsimon.util.MySecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
public class AccountHandler {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AdminRepository adminRepository;

    @Value(value = "${server.port}")
    private String port;

    @GetMapping("/index")
    public String index() {
        return "端口号：" + this.port;
    }

    @GetMapping("/login/{userName}/{password}/{type}")
    public Account login(@PathVariable("userName") String userName, @PathVariable("password") String password, @PathVariable("type") String type) {
        Account account = new Account();
        switch (type) {
            case "user":
                account = userRepository.findAll(userName, MySecurity.encryptUserPassword(password));
                break;
            case "admin":
                account = adminRepository.findAll(userName, MySecurity.encryptUserPassword(password));
                break;
            default:
                break;
        }
        return account;
    }
}
